/*
 * @Date: 2020-05-09 17:42:50
 * @Description: 通用方法,也可直接用lodash中方法
 * 
 * 
 * 引用
 * import * as util from '@/utils/utils'
 * util.debounce
 * 或
 * import {debounce} from '@/utils/utils'
 * 
 * @FilePath: \vue-leng\src\utils\utils.js
 */ 
//公共方法

/**
 * @description: 类型检查
 * Type.Array(xx)
 */
const Type = (function () {
  let type = {}
  let typeArr = ['String', 'Object', 'Number', 'Array', 'Undefined', 'Function', 'Null', 'Symbol', 'HTMLElement']
  for (let i = 0; i < typeArr.length; i++) {
    (function (name) {
      type['is' + name] = function (obj) {
        return Object.prototype.toString.call(obj) == '[object ' + name + ']'
      }
    })(typeArr[i])
  }
  return type
})()


// 
const compose = function (...fns) {
  if (fns.length === 0) return arg => arg;
  if (fns.length === 1) return fns[0];
  return fns.reduce((a, b) => (...args) => a(b(...args)))
}

/**
 * 防抖
 * @param fun
 * @param delay
 * @returns {Function}
 */
const debounce = function (fun, delay) {
  let timer = null
  return function (args) {
    let that = this
    let _args = args
    clearTimeout(timer)
    timer = setTimeout(function () {
      fun.call(that, _args)
    }, delay)
  }
}

/**
 * 节流
 * @param fun
 * @param delay
 * @return {Function}
 */
const throttle = function (fun, delay) {
  let last, deferTimer
  return function (args) {
    let that = this
    let _args = arguments
    let now = +new Date()
    if (last && now < last + delay) {
      clearTimeout(deferTimer)
      deferTimer = setTimeout(function () {
        last = now
        fun.apply(that, _args)
      }, delay)
    } else {
      last = now
      fun.apply(that, _args)
    }
  }
}

// 添加script
const loadScript = function (url, callback) {
  let script = document.createElement('script')
  script.setAttribute('type', 'text/javascript')
  script.onload = script.onreadystatechange = function () {
    callback && callback(!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')
  }
  script.onerror = function (res) {
    callback && callback(false)
  }
  script.setAttribute('src', url)
  document.body.appendChild(script)
}

/**
 * 判断当前设备
 * @returns {string}
 */
const isIosOrAndroid = function () {
  let u = navigator.userAgent
  let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  let isStr = ''
  if (isAndroid) {
    isStr = 'android'
  }
  if (isiOS) {
    isStr = 'ios'
  }
  return isStr
}

// 删除左右两端的空格
const trim = function (str) {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}

// 删除左边的空格
const ltrim = function (str) {
  return str.replace(/(^\s*)/g, '')
}

// 删除右边的空格
const rtrim = function (str) {
  return str.replace(/(\s*$)/g, '')
}

// 将数组切割成指定的若干数组块
const chunk = function (arr, size) {
  var arr2 = []
  for (var i = 0; i < arr.length; i = i + size) {
    arr2.push(arr.slice(i, i + size))
  }
  return arr2
}
// 数组去重
function arrayHeavy (arr) {
  var res = []
  var json = {}
  for (var i = 0; i < arr.length; i++) {
    if (!json[arr[i]]) {
      res.push(arr[i])
      json[arr[i]] = 1
    }
  }
  return res
}
// 去除数组中的undefined
const notEmpty = function (arr) {
  if (arr && arr.constructor === Array) {
    return arr.filter(t => t !== undefined && t !== null)
  }
  return []
}

/**
 * 深拷贝
 * @param {*} data
 * @returns
 */
function deepCopy (data) {
  const t = typeOf(data)
  let o
  if (t === 'array') {
    o = []
  } else if (t === 'object') {
    o = {}
  } else {
    return data
  }
  if (t === 'array') {
    for (let i = 0; i < data.length; i++) {
      o.push(deepCopy(data[i]))
    }
  } else if (t === 'object') {
    for (let i in data) {
      o[i] = deepCopy(data[i])
    }
  }
  return o
}


export{
  Type,
  compose,
  debounce,
  throttle,
  loadScript,
  isIosOrAndroid,
  trim,
  ltrim,
  rtrim,
  chunk,
  notEmpty,
  arrayHeavy,
  deepCopy
}